package com.trxy.hotelsystem.contorller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.trxy.hotelsystem.model.clientInf;
import com.trxy.hotelsystem.model.staffInf;
import com.trxy.hotelsystem.service.impl.LonginserviceImpl;
import com.trxy.hotelsystem.service.impl.VipServiceImpl;

@Controller
public class LoginController {
	
	@Autowired
	private LonginserviceImpl longinserviceImpl;
	
	@Autowired
	private VipServiceImpl VipServiceimpl;
	
	//员工登录
	@RequestMapping(value = "/login.do")
	public void Log(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			Model model,staffInf stafInf) throws Exception{
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		//获取登录用户的数据
		String userNumber = request.getParameter("userName");
		String passWord = request.getParameter("password");
		
		stafInf.setNumber(userNumber);
		
		//访问服务层
		List<staffInf> list = longinserviceImpl.selectStafInfBynumb(stafInf);
		
		if(list.size() > 0 ) {
			for(staffInf one:list) {  
				if(one.getPassword().equals(passWord)) {//登录成功
					//把登录用户存到session中
					session.setAttribute("loginUser", one);
					session.setMaxInactiveInterval(60*60*60);
					pw.write("1");
				}else {             // 密码错误登录失败
					pw.write("2");
				}
			}

		}else {
//			账号不存在，登录失败
			pw.write("0");
		}
		
		
	}
	
	
	//会员登录
		@RequestMapping(value = "/viplogin.do")
		public void vipLog(HttpServletRequest request, HttpServletResponse response, HttpSession session,
				Model model,clientInf clientinf) throws Exception{
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			PrintWriter pw = response.getWriter();
			
			//获取登录用户的数据
			String userNumber = request.getParameter("userName");
			String passWord = request.getParameter("password");
			
			clientinf.setNumber(userNumber);
			
			//访问服务层
			List<clientInf> list = VipServiceimpl.selectVipBynumber(userNumber);
			
			if(list.size() > 0 ) {
				for(clientInf one:list) {  
					if(one.getPassword().equals(passWord)) {//登录成功
						//把登录用户存到session中
						session.setAttribute("viplogin", one);
						session.setMaxInactiveInterval(60*60*60);
						pw.write("1");
					}else {             // 密码错误登录失败
						pw.write("2");
					}
				}

			}else {
//				账号不存在，登录失败
				pw.write("0");
			}
			
			
		}

}
